<?php
namespace app\common\model;

use think\model\concern\SoftDelete;
use think\Model;

class Auth extends Model
{
    use SoftDelete;

    //定义主键
    protected $pk = 'auth_id';

    //定义模型对应的表名
    protected $table = 'auths';

    //定义软删除的字段
    protected $deleteTime = 'deleted_time';

    public static $module_name_cn_array       = array();
    public static $controller_name_cn_array   = array();
    public static $action_name_cn_array       = array();

    //自定义初始化
    protected static function init()
    {
        //获取config/Auth.php中的所有配置
        $configs = config('auth.');

        $modules = array_column($configs, 'module_name_cn', 'module_name');
        self::$module_name_cn_array = $modules;

        $controllers = array_column($configs, 'controllers', 'module_name');
        //控制器
        foreach ($controllers as $key => $controller) {
            $temp[$key] = array_column($controller, 'controller_name_cn', 'controller_name');
            self::$controller_name_cn_array = $temp;

            //三维数组
            $action_temp[$key] = array_column($controller, 'actions', 'controller_name');
            self::$action_name_cn_array = $action_temp;
        }
    }

    //定义与icons表的关联关系
    protected function icon()
    {
        return $this->belongsTo('Icon', 'icon_id', 'icon_id');
    }


    protected function getIconIdLabelAttr($value, $data)
    {
        $icon = Icon::get($data['icon_id']);

        return $icon['icon'];
    }

    protected function getEnabledLabelAttr($value, $data)
    {
        $enabled = array(
            0   => '隐藏',
            1   => '展示'
        );

        return $enabled[$data['enabled']];
    }



    protected function getLevelLabelAttr($value, $data)
    {
        $level_array = array(
            1   => '一级',
            2   => '二级',
            3   => '三级',
            4   => '四级',
        );

        return $level_array[$data['level']];
    }


    protected function getModuleNameCnAttr($value, $data)
    {
        $module_name_cn = '';
        if (empty($data)) {
            return '';
        }
        if (in_array($data['module_name'], array_keys(self::$module_name_cn_array))) {
           $module_name_cn = self::$module_name_cn_array[$data['module_name']];
        }

        return $module_name_cn;
    }


    protected function getControllerNameCnAttr($value, $data)
    {
        $controller_name_cn = '';
        if (empty($data)) {
            return '';
        }
        if (in_array($data['controller_name'], array_keys(self::$controller_name_cn_array[$data['module_name']]))) {
           $controller_name_cn = self::$controller_name_cn_array[$data['module_name']][$data['controller_name']];
        }

        return $controller_name_cn;
    }

    protected function getActionNameCnAttr($value, $data)
    {
        $action_name_cn = '';
        if (empty($data)) {
            return '';
        }
        if ($data['action_name'] == 'index') {
            return $data['name'];
        }


        if (in_array($data['controller_name'], array_keys(self::$action_name_cn_array[$data['module_name']]))) {
            $temp = self::$action_name_cn_array[$data['module_name']][$data['controller_name']];

            if (in_array($data['action_name'], array_keys($temp))) {
                $action_name_cn = $temp[$data['action_name']];
            }

        }
        return $action_name_cn;
    }

}